/**
 * @package Infinity-Seed
 * @subpackage view
 *
 * @copyright Copyright (C) 2012-2013 Exia.CESI
 * @author Paul MAGNIN, Arthur MERCIER, Jordan RICHET, Cyril SCHUMACHER
 */
package view;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.*;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * Fenêtre contenant les statistiques.
 * @author Cyril SCHUMACHER <cyril.schumacher at viacesi.fr>
 */
public class StatsWindow {
    // <editor-fold defaultstate="collapsed" desc="Attributs.">
    /**
     * Fenêtre.
     */
    protected JFrame frame;
    /**
     * Position de la fenêtre de simulation.
     */
    private Point point = new Point();
    /**
     * Configuration.
     */
    private infinity.seed.Configuration _configuration;
    /**
     * Nombre de pas.
     */
    private JLabel _stepNumberLabel;
    /**
     * Temps entre chaque pas.
     */
    private JLabel _timeNumberLabel;
    /**
     * Nombre de case vide.
     */
    private JLabel _emptyNumberLabel;
    /**
     * Nombre de jeune pousse.
     */
    private JLabel _seedNumberLabel;
    /**
     * Nombre d'arbuste.
     */
    private JLabel _bushNumberLabel;
    /**
     * Nombre d'arbre.
     */
    private JLabel _treeNumberLabel;
    /**
     * Nombre d'infectés.
     */
    private JLabel _infectedNumberLabel;
    /**
     * Nombre en feu.
     */
    private JLabel _inFiredNumberLabel;
    /**
     * Nombre de cendre.
     */
    private JLabel _ashedNumberLabel;
    /**
     * Nombre de pas.
     */
    private Integer _numberStep;
    /**
     * Temps entre chaque pas.
     */
    private Integer _numberTime;
    /**
     * Fenêtre contenant la grille.
     */
    private GridWindow _gridWindow;
    /**
     * Fenêtre SimulatorWindow.
     */
    private SimulatorWindow _simulatorWindow;
    /**
     * Exportation des statistiques dans un fichier CSV.
     */
    private infinity.seed.ExportCSV _export;
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Méthodes.">
    // <editor-fold defaultstate="collapsed" desc="Constructeur.">
    /**
     * Constructeur.
     */
    public StatsWindow(SimulatorWindow simulatorWindow, GridWindow gridWindow) {
        // Récupère une instance de la classe Configuration.
        this._configuration = infinity.seed.Configuration.getInstance();
        // Créer la fenêtre.
        this.frame = new JFrame(this._configuration.get("stats.title"));
        
        // Sauvegarde l'instance des fenêtres.
        this._simulatorWindow = simulatorWindow;
        this._gridWindow = gridWindow;
        
        // Initialise les variables contenant le temps entre chaque pas et le nombre de pas.
        this._numberStep = Integer.parseInt(this._configuration.get("grid.step"));
        this._numberTime = Integer.parseInt(this._configuration.get("grid.time"));
        
        // Créer une instance pour la sauvegarde des statistiques au format CSV.
        this._export = new infinity.seed.ExportCSV();

        // Empêche le client de fermer et de redimensionner la fenêtre.
        this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.frame.setResizable(false);
        this.frame.setBackground(Color.decode(_configuration.get("jwindow.background")));
        this.frame.setUndecorated(true);

        // Initialise les composants.
        this._InitializeComponent();

        // Redimensionne selon les composants, positionne et affiche la fenêtre.
        this.frame.pack();
        this.frame.setVisible(true);
        this.frame.setLocation(20,305);
        this.frame.setSize(402,280);
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Destructeur.">
    /**
     * Destructeur.
     * @throws Throwable 
     */
    @Override public void finalize() throws Throwable {
        super.finalize();
        this.frame.dispose();
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Initialise les composants.">
    /**
     * Initialise les composants.
     */
    private void _InitializeComponent() {
        // <editor-fold defaultstate="collapsed" desc="Initialisation.">
        // Utilisation de police personnalisée.
        infinity.seed.CustomFont customFont = new infinity.seed.CustomFont();

        // Lie les événements à la fenêtre.
        StatsWindowEvent windowEvent = new StatsWindowEvent();
        this.frame.addMouseListener(windowEvent);
        this.frame.addMouseMotionListener(windowEvent);

        // Ajoute une disposition au contenu principale.
        BorderLayout contentPaneLayout = new BorderLayout();
        this.frame.getContentPane().setLayout(contentPaneLayout);
        this.frame.getRootPane().setBorder(BorderFactory.createLineBorder(Color.black));
        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Barre de titre.">
        // Barre de titre.
        JPanel titlePanel = new JPanel();
        this.frame.getContentPane().add(titlePanel, BorderLayout.PAGE_START);
        titlePanel.setBackground(Color.decode(_configuration.get("jwindow.background")));
        titlePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JPanel spacePanel = new JPanel();
        spacePanel.setPreferredSize(new Dimension(0, 5));
        titlePanel.add(spacePanel);
        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Contenu de la fenêtre.">
        int informationsPanelWidth = 300;
        int informationsPanelHeight = 25;

        JPanel informationsPanel = new JPanel();
        BoxLayout informationsLayout = new BoxLayout(informationsPanel, BoxLayout.Y_AXIS);
        informationsPanel.setLayout(informationsLayout);
        informationsPanel.setBorder(BorderFactory.createEmptyBorder(2,20,20,0));
        this.frame.getContentPane().add(informationsPanel, BorderLayout.WEST);

        JPanel statsPanel = new JPanel();
        statsPanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(statsPanel);

        JLabel statsLabel = new JLabel(this._configuration.get("statsLabel.text"));
        statsLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Semibold.ttf", 24));
        statsPanel.add(statsLabel);
        
        /**
         * *******************************************************
         * GRILLE.
         * *******************************************************
         */
        JPanel gridPanel = new JPanel();
        gridPanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        gridPanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(gridPanel);

        JLabel gridLabel = new JLabel(this._configuration.get("gridLabel.text"));
        gridLabel.setPreferredSize(new Dimension(100, 15));
        gridLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        gridPanel.add(gridLabel);
        JLabel gridNumberLabel = new JLabel(_gridWindow.grid.length + "x" + _gridWindow.grid[0].length);
        gridNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        gridPanel.add(gridNumberLabel);

        /**
         * *******************************************************
         * NOMBRE DE PAS.
         * *******************************************************
         */
        JPanel stepPanel = new JPanel();
        stepPanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        stepPanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(stepPanel);

        JLabel stepLabel = new JLabel(this._configuration.get("stepLabel.text"));
        stepLabel.setPreferredSize(new Dimension(100, 15));
        stepLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        stepPanel.add(stepLabel);
        _stepNumberLabel = new JLabel(this._numberStep.toString());
        _stepNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        stepPanel.add(_stepNumberLabel);

        /**
         * *******************************************************
         * TEMPS ENTRE CHAQUE PAS.
         * *******************************************************
         */
        JPanel timePanel = new JPanel();
        timePanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        timePanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(timePanel);

        JLabel timeLabel = new JLabel(this._configuration.get("timeLabel.text"));
        timeLabel.setPreferredSize(new Dimension(150, 15));
        timeLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        timePanel.add(timeLabel);
        _timeNumberLabel = new JLabel(this._numberTime.toString());
        _timeNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        timePanel.add(_timeNumberLabel);

        /**
         * *******************************************************
         * VIDES.
         * *******************************************************
         */
        JPanel emptyPanel = new JPanel();
        emptyPanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        emptyPanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(emptyPanel);

        JLabel emptyLabel = new JLabel(this._configuration.get("stats.emptyLabel.text"));
        emptyLabel.setPreferredSize(new Dimension(100, 15));
        emptyLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        emptyPanel.add(emptyLabel);
        _emptyNumberLabel = new JLabel("1.0");
        _emptyNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        _emptyNumberLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
        emptyPanel.add(_emptyNumberLabel);

        /**
         * *******************************************************
         * JEUNES POUSSES.
         * *******************************************************
         */
        JPanel seedPanel = new JPanel();
        seedPanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        seedPanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(seedPanel);

        JLabel seedLabel = new JLabel(this._configuration.get("stats.seedLabel.text"));
        seedLabel.setPreferredSize(new Dimension(100, 15));
        seedLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        seedPanel.add(seedLabel);
        _seedNumberLabel = new JLabel("0.0");
        _seedNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        _seedNumberLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
        seedPanel.add(_seedNumberLabel);

        /**
         * *******************************************************
         * ARBUSTES. *******************************************************
         */
        JPanel bushPanel = new JPanel();
        bushPanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        bushPanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(bushPanel);

        JLabel bushLabel = new JLabel(this._configuration.get("stats.bushLabel.text"));
        bushLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        bushPanel.add(bushLabel);
        _bushNumberLabel = new JLabel("0.0");
        _bushNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        _bushNumberLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
        bushPanel.add(_bushNumberLabel);

        /**
         * *******************************************************
         * ARBRES. 
         * *******************************************************
         */
        JPanel treePanel = new JPanel();
        treePanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        treePanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(treePanel);

        JLabel treeLabel = new JLabel(this._configuration.get("stats.treeLabel.text"));
        treeLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        treePanel.add(treeLabel);
        _treeNumberLabel = new JLabel("0.0");
        _treeNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        _treeNumberLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
        treePanel.add(_treeNumberLabel);

        /**
         * *******************************************************
         * INFECTES.
         * *******************************************************
         */
        JPanel infectedPanel = new JPanel();
        infectedPanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        infectedPanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(infectedPanel);

        JLabel infectedLabel = new JLabel(this._configuration.get("stats.infectedLabel.text"));
        infectedLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        infectedPanel.add(infectedLabel);
        _infectedNumberLabel = new JLabel("0.0");
        _infectedNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        _infectedNumberLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
        infectedPanel.add(_infectedNumberLabel);

        /**
         * *******************************************************
         * EN FEU. 
         * *******************************************************
         */
        JPanel inFirePanel = new JPanel();
        inFirePanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        inFirePanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(inFirePanel);

        JLabel inFireLabel = new JLabel(this._configuration.get("stats.inFireLabel.text"));
        inFireLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        inFirePanel.add(inFireLabel);
        _inFiredNumberLabel = new JLabel("0.0");
        _inFiredNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        _inFiredNumberLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
        inFirePanel.add(_inFiredNumberLabel);

        /**
         * *******************************************************
         * CENDRE. 
         * *******************************************************
         */
        JPanel ashedPanel = new JPanel();
        ashedPanel.setMaximumSize(new Dimension(informationsPanelWidth, informationsPanelHeight));
        ashedPanel.setLayout(new GridLayout(0, 2));
        informationsPanel.add(ashedPanel);

        JLabel ashedLabel = new JLabel(this._configuration.get("stats.ashedLabel.text"));
        ashedLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        ashedPanel.add(ashedLabel);
        _ashedNumberLabel = new JLabel("0.0");
        _ashedNumberLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 12));
        _ashedNumberLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
        ashedPanel.add(_ashedNumberLabel);
        
        JPanel navigatePanel = new JPanel();
        navigatePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.frame.getContentPane().add(navigatePanel, BorderLayout.PAGE_END);
        
        // Bouton "Exporter".
        JButton exportCSVButton = new JButton(this._configuration.get("exportCSVButton.text").toUpperCase());
        exportCSVButton.setToolTipText(this._configuration.get("exportCSVButton.tooltip"));
        exportCSVButton.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 15));
        exportCSVButton.setBackground(Color.decode(_configuration.get("jbutton.background")));
        exportCSVButton.setForeground(Color.decode(_configuration.get("jbutton.text")));
        exportCSVButton.setBorder(BorderFactory.createEmptyBorder(5, 25, 5, 25));
        exportCSVButton.addMouseListener(new ButtonEvent());
        exportCSVButton.addMouseListener(new ExportButtonEvents());
        navigatePanel.add(exportCSVButton);
        
        JButton saveGridButton = new JButton(this._configuration.get("saveGridButton.text").toUpperCase());
        saveGridButton.setToolTipText(this._configuration.get("saveGridButton.tooltip"));
        saveGridButton.setFont(customFont.getFont("/resources/open-sans/OpenSans-Bold.ttf", 15));
        saveGridButton.setBackground(Color.decode(_configuration.get("jbutton.background")));
        saveGridButton.setForeground(Color.decode(_configuration.get("jbutton.text")));
        saveGridButton.setBorder(BorderFactory.createEmptyBorder(5, 25, 5, 25));
        saveGridButton.addMouseListener(new ButtonEvent());
        saveGridButton.addMouseListener(new SaveGridButtonEvents());
        navigatePanel.add(saveGridButton);
        // </editor-fold>
    } 
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Récupère la couleur selon la nouvelle et ancienne densité.">
    /**
     * Récupère la couleur selon la nouvelle et ancienne densité.
     * @param densityOld Ancienne densité.
     * @param densityNew Nouvelle densité.
     * @return Retourne la couleur adéquat.
     */
    private Color _getColorDensity(Float densityOld, Float densityNew) {
        if (densityOld < densityNew) {
            return Color.decode(_configuration.get("stats.growth"));
        } else if(densityOld > densityNew) {
            return Color.decode(_configuration.get("stats.decreasing"));
        } else {
            return Color.BLACK;
        }
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Met à jour les statistiques.">
    /**
     * Met à jour les statistiques.
     */
    protected void updateStats() {
        // Récupère le nombre total de case contenu dans la grille.
        Float caseTotal = (float) (_gridWindow.grid.length * _gridWindow.grid[0].length);
        
        // Met à jour le nombre de pas et le temps entre chaque pas.
        this._stepNumberLabel.setText(this._numberStep.toString());
        this._timeNumberLabel.setText(this._numberTime.toString());

        // Calcule la densite pour :
        // Vide.
        Float densityEmpty = ((float) this._gridWindow.gridCell.getEmptyNumber()) / caseTotal;
        this._emptyNumberLabel.setForeground(this._getColorDensity(Float.parseFloat(this._emptyNumberLabel.getText()), densityEmpty));
        this._emptyNumberLabel.setText(densityEmpty.toString());
        this._export.empty.add(densityEmpty);
        // Jeune pousse.
        Float densitySeed = ((float) this._gridWindow.gridCell.getSeedNumber()) / caseTotal;
        this._seedNumberLabel.setForeground(this._getColorDensity(Float.parseFloat(this._seedNumberLabel.getText()), densitySeed));
        this._seedNumberLabel.setText(densitySeed.toString());
        this._export.seed.add(densitySeed);
        // Arbuste.
        Float densityBush = ((float) this._gridWindow.gridCell.getBushNumber()) / caseTotal;
        this._bushNumberLabel.setForeground(this._getColorDensity(Float.parseFloat(this._bushNumberLabel.getText()), densityBush));
        this._bushNumberLabel.setText(densityBush.toString());
        this._export.bush.add(densityBush);
        // Arbre.
        Float densityTree = ((float) this._gridWindow.gridCell.getTreeNumber()) / caseTotal;
        this._treeNumberLabel.setForeground(this._getColorDensity(Float.parseFloat(this._treeNumberLabel.getText()), densityTree));
        this._treeNumberLabel.setText(densityTree.toString());
        this._export.tree.add(densityTree);
        // En feu.
        Float densityInFired = ((float) this._gridWindow.gridCell.getInfiredNumber()) / caseTotal;
        this._inFiredNumberLabel.setForeground(this._getColorDensity(Float.parseFloat(this._inFiredNumberLabel.getText()), densityInFired));
        this._inFiredNumberLabel.setText(densityInFired.toString());
        // Infecté.
        Float densityInfected = ((float) this._gridWindow.gridCell.getInfectedNumber()) / caseTotal;
        this._infectedNumberLabel.setForeground(this._getColorDensity(Float.parseFloat(this._infectedNumberLabel.getText()), densityInfected));
        this._infectedNumberLabel.setText(densityInfected.toString());
        // Cendre.
        Float densityAshed = ((float) this._gridWindow.gridCell.getAshedNumber()) / caseTotal;
        this._ashedNumberLabel.setForeground(this._getColorDensity(Float.parseFloat(this._ashedNumberLabel.getText()), densityAshed));
        this._ashedNumberLabel.setText(densityAshed.toString());
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Récupère le nombre de pas.">
    /**
     * Récupère le nombre de pas.
     * @return Retourne un entier.
     */
    public Integer getStepNumber() {
        return this._numberStep;
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Définit le nombre de pas.">
    /**
     * Définit le nombre de pas.
     * @param stepNumber Nombre de pas.
     */
    public void setStepNumber(Integer stepNumber) {
        this._numberStep = stepNumber;
        // Met à jour les statistiques.
        this.updateStats();
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Récupère le temps entre chaque pas.">
    /**
     * Récupère le temps entre chaque pas.
     * @return Retourne un entier.
     */
    public Integer getTimeNumber() {
        return this._numberTime;
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Définit le temps entre chaque pas.">
    /**
     * Définit le temps entre chaque pas.
     * @param stepNumber Nombre de pas.
     */
    public void setTimeNumber(Integer timeNumber) {
        this._numberTime = timeNumber;
        // Met à jour les statistiques.
        this.updateStats();
    }
    // </editor-fold>
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Evénements.">
    // <editor-fold defaultstate="collapsed" desc="Gestion des évènements de la fenêtre.">
    /**
     * Gestion des évènements de la fenêtre.
     */
    private class StatsWindowEvent extends MouseAdapter {

        /**
         * Recupere la position du curseur.
         *
         * @param e Parametres.
         */
        @Override
        public void mousePressed(MouseEvent e) {
            point.x = e.getX();
            point.y = e.getY();
        }

        /**
         * Selon la position du curseur, on déplace la fenêtre.
         *
         * @param e Parametres.
         */
        @Override
        public void mouseDragged(MouseEvent e) {
            JFrame frame = (JFrame) e.getSource();
            Point position = frame.getLocation();
            frame.setLocation(position.x + e.getX() - point.x, position.y + e.getY() - point.y);
        }
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Gestion des évènements pour le bouton "Exporter les statistiques".">
    /**
     * Gestion des évènements pour le bouton "Exporter les statistiques".
     */
    private class ExportButtonEvents extends MouseAdapter {
        @Override public void mouseClicked(MouseEvent e) {
            _export.save(frame);
        }
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Gestion des évènements pour le bouton "Exporter la grille".">
    /**
     * Gestion des évènements pour le bouton "Exporter la grille".
     */
    private class SaveGridButtonEvents extends MouseAdapter {
        @Override public void mouseClicked(MouseEvent e) {
            // Créer la fenêtre de choix du chemin pour enregistrer l'image.
            JFileChooser filePNG = new JFileChooser();
            int option = filePNG.showSaveDialog(_gridWindow.frame);
            
            // Si le client a choisit de sauvegarder dans le répertoire souhaité.
            if (option == JFileChooser.APPROVE_OPTION)
            {
                // Récupère l'image et l'a place dans un buffer.
                BufferedImage image = new BufferedImage(_gridWindow.gridCell.getWidth(), _gridWindow.gridCell.getHeight(), BufferedImage.TYPE_INT_ARGB);
                _gridWindow.gridCell.paint(image.getGraphics());
               
                // Récupère le chemin d'accès à l'image.
                File fileName = new File (filePNG.getSelectedFile() + ".png");
          
                try {
                    // Sauvegarde les données dans une image au format PNG.
                    ImageIO.write(image, "PNG", new File(fileName.getPath()));
                } catch (IOException ex) {
                    Logger.getLogger(SimulatorWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    // </editor-fold>
    // </editor-fold>
}
